local sk__tiangong = fk.CreateSkill {

  name = "sk__tiangong",

  tags = {},

}
local sk__tiangong_skills = {}
for loop = 1, 60, 1 do
  local m, n = loop % 10, loop % 12
  if m == 0 then
    m = 10
  end
  if n == 0 then
    n = 12
  end
  table.insert(sk__tiangong_skills, "sk__tiangong_create" .. m .. n)
end
local tiangong_info = {
  events = {
    "准备阶段开始时",
    "判定阶段开始时",
    "摸牌阶段开始时",
    "出牌阶段开始时",
    "出牌阶段结束时",
    "弃牌阶段开始时",
    "弃牌阶段结束时",
    "结束阶段开始时",
    "每回合限1~3次，当你的判定牌生效后",
    "每回合限1~3次，当你获得牌后",
    "每回合限1~3次，当你使用基本牌后",
    "每回合限1~3次，当你使用锦囊牌后",
    "每回合限1~3次，当你使用装备牌后",
    "每回合限1~3次，当你使用红色牌后",
    "每回合限1~3次，当你使用黑色牌后",
    "每回合限1~3次，当你成为普通锦囊牌的目标后",
    "每回合限1~3次，当你失去装备区内的牌后",
    "每回合限1~3次，当你成为【杀】的目标时",
    "每回合限1~3次，当你成为【杀】的目标后",
    "每回合限1~3次，当你使用或打出【杀】时",
    "每回合限1~3次，当你使用或打出【闪】时",
    "每回合限1~3次，当你造成伤害时",
    "每回合限1~3次，当你造成伤害后",
    "每回合限1~3次，当你受到伤害时",
    "每回合限1~3次，当你受到伤害后",
    "每回合限1~3次，当你回复体力或增加体力上限后",
    "每回合限1~3次，当你失去体力或减少体力上限后",
    "每回合限1~3次，当你进入濒死状态时",
    "每回合限1~3次，当你脱离濒死状态后",
    "每回合限1~3次，当你获得技能后",
    "每回合限1~3次，当你失去技能后",
    "每回合限1~3次，当你横置或重置或翻面后",
  },
  targets = {
    "你",
    "所有角色",
    "所有其他角色",
    "所有男性角色",
    "所有女性角色",
    "所有魏势力角色",
    "所有蜀势力角色",
    "所有吴势力角色",
    "所有群势力角色",
    "所有其他势力角色",
    "随机一名角色",
    "随机两名角色",
    "随机三名角色",
    "随机任意名角色",
    "体力上限最多的角色",
    "体力上限最少的角色",
    "体力值最多的角色",
    "体力值最少的角色",
    "手牌最多的角色",
    "手牌最少的角色",
    "随机一名未受伤角色",
    "随机两名未受伤角色",
    "所有未受伤角色",
    "随机一名已受伤角色",
    "随机两名已受伤角色",
    "所有已受伤角色",
  },
  effects = {
    "翻面",
    "进行一次【闪电】判定",
    "手牌上限+1",
    "出牌阶段使用【杀】次数上限+1",
    "随机失去一个技能",
    "随机获得一个魏势力武将技能",
    "随机获得一个蜀势力武将技能",
    "随机获得一个吴势力武将技能",
    "随机获得一个群势力武将技能",
    "随机获得一个其他势力武将技能",
    "视为使用【南蛮入侵】",
    "视为使用【万箭齐发】",
    "视为使用【桃园结义】",
    "视为使用【五谷丰登】",
    "视为使用【无中生有】",
    "视为使用【桃】",
    "视为对所有其他角色使用【杀】",
    "视为对所有其他角色使用雷【杀】",
    "视为对所有其他角色使用火【杀】",
    "视为对所有其他角色使用【决斗】",
    "从牌堆或弃牌堆随机获得两张红色牌",
    "从牌堆或弃牌堆随机获得两张黑色牌",
    "从牌堆或弃牌堆随机获得两张基本牌",
    "从牌堆或弃牌堆随机获得两张锦囊牌",
    "从牌堆或弃牌堆随机获得两张装备牌",
    "受到1点伤害",
    "受到1点雷电伤害",
    "受到1点火焰伤害",
    "对所有其他角色各造成1点伤害",
    "加1点体力上限",
    "减1点体力上限",
    "回复1点体力",
    "回复2点体力",
    "失去1点体力",
    "失去2点体力",
    "失去1点体力并摸五张牌",
    "摸两张牌",
    "摸三张牌",
    "摸四张牌",
    "随机弃置两张牌",
    "随机弃置三张牌",
    "随机弃置四张牌",
    "弃置所有牌并摸等量的牌",
    "随机获得所有其他角色各一张牌",
    "与随机一名手牌数更多的角色交换手牌",
    "与随机一名手牌数更少的角色交换手牌",
  }
}

sk__tiangong:addEffect(fk.GameStart, {
  name = "sk__tiangong",
  anim_type = "special",
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(sk__tiangong.name) and
        (not player.room.tag["sk__tiangong_skills"] or #player.room.tag["sk__tiangong_skills"] < 7) then
      return true
    end
  end,
  on_trigger = function(self, event, target, player, data)
    self.cancel_cost = false
    for i = 1, 2, 1 do
      if not player:hasSkill(sk__tiangong.name) or self.cancel_cost then return end
      self:doCost(event, target, player, data)
    end
  end,
  on_cost = function(self, event, target, player, data)
    if player.room:askForSkillInvoke(player, sk__tiangong.name) then
      return true
    end
    self.cancel_cost = true
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    --初始化随机数
    math.randomseed(tonumber(tostring(os.time()):reverse():sub(1, 6)))
    --时机
    local nums = {}
    for i = 1, 32, 1 do
      table.insert(nums, i)
    end
    nums = table.random(nums, 3)
    local choices = {
      tiangong_info.events[nums[1]],
      tiangong_info.events[nums[2]],
      tiangong_info.events[nums[3]],
    }
    local choice_event = room:askForChoice(player, choices, sk__tiangong.name,
      "#sk__tiangong-choice_event")
    --目标
    nums = {}
    for i = 1, 26, 1 do
      table.insert(nums, i)
    end
    nums = table.random(nums, 3)
    choices = {
      tiangong_info.targets[nums[1]],
      tiangong_info.targets[nums[2]],
      tiangong_info.targets[nums[3]],
    }
    local choice_target = room:askForChoice(player, choices, sk__tiangong.name,
      "#sk__tiangong-choice_target:::" .. choice_event)
    --效果
    nums = {}
    for i = 1, 46, 1 do
      table.insert(nums, i)
    end
    nums = table.random(nums, 3)
    choices = {
      tiangong_info.effects[nums[1]],
      tiangong_info.effects[nums[2]],
      tiangong_info.effects[nums[3]],
    }
    local choice_effect = room:askForChoice(player, choices, sk__tiangong.name,
      "#sk__tiangong-choice_effect:::" .. choice_event .. ":" .. choice_target)
    --选获得技能的角色
    local to = room:askForChoosePlayers(player, table.map(room.alive_players, Util.IdMapper), 1, 1,
      "#sk__tiangong-choose:::" .. choice_event .. ":" .. choice_target .. ":" .. choice_effect, sk__tiangong.name, false)
    to = room:getPlayerById(to[1])
    --获得技能名
    room.tag["sk__tiangong_skills"] = room.tag["sk__tiangong_skills"] or {}
    local name = table.random(sk__tiangong_skills)
    while table.contains(room.tag["sk__tiangong_skills"], name) do
      name = table.random(sk__tiangong_skills)
    end
    --房间记录技能信息
    table.insert(room.tag["sk__tiangong_skills"], name)
    local tiangong_scope = 999
    if table.indexOf(tiangong_info.events, choice_event) > 8 then
      tiangong_scope = math.random(3)
    end
    local mark = {
      table.indexOf(tiangong_info.events, choice_event),
      table.indexOf(tiangong_info.targets, choice_target),
      table.indexOf(tiangong_info.effects, choice_effect),
      tiangong_scope,
    }
    room.tag[name] = mark
    room:handleAddLoseSkills(to, name, nil, true, false)
  end,
})
sk__tiangong:addEffect(fk.TurnStart, {
  name = "sk__tiangong",
  anim_type = "special",
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(sk__tiangong.name) and
        (not player.room.tag["sk__tiangong_skills"] or #player.room.tag["sk__tiangong_skills"] < 7) then
      return target == player
    end
  end,
  on_trigger = function(self, event, target, player, data)
    self.cancel_cost = false
    if event == fk.GameStart then
      for i = 1, 2, 1 do
        if not player:hasSkill(sk__tiangong.name) or self.cancel_cost then return end
        self:doCost(event, target, player, data)
      end
    else
      self:doCost(event, target, player, data)
    end
  end,
  on_cost = function(self, event, target, player, data)
    if player.room:askForSkillInvoke(player, sk__tiangong.name) then
      return true
    end
    self.cancel_cost = true
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    --初始化随机数
    math.randomseed(tonumber(tostring(os.time()):reverse():sub(1, 6)))
    --时机
    local nums = {}
    for i = 1, 32, 1 do
      table.insert(nums, i)
    end
    nums = table.random(nums, 3)
    local choices = {
      tiangong_info.events[nums[1]],
      tiangong_info.events[nums[2]],
      tiangong_info.events[nums[3]],
    }
    local choice_event = room:askForChoice(player, choices, sk__tiangong.name,
      "#sk__tiangong-choice_event")
    --目标
    nums = {}
    for i = 1, 26, 1 do
      table.insert(nums, i)
    end
    nums = table.random(nums, 3)
    choices = {
      tiangong_info.targets[nums[1]],
      tiangong_info.targets[nums[2]],
      tiangong_info.targets[nums[3]],
    }
    local choice_target = room:askForChoice(player, choices, sk__tiangong.name,
      "#sk__tiangong-choice_target:::" .. choice_event)
    --效果
    nums = {}
    for i = 1, 46, 1 do
      table.insert(nums, i)
    end
    nums = table.random(nums, 3)
    choices = {
      tiangong_info.effects[nums[1]],
      tiangong_info.effects[nums[2]],
      tiangong_info.effects[nums[3]],
    }
    local choice_effect = room:askForChoice(player, choices, sk__tiangong.name,
      "#sk__tiangong-choice_effect:::" .. choice_event .. ":" .. choice_target)
    --选获得技能的角色
    local to = room:askForChoosePlayers(player, table.map(room.alive_players, Util.IdMapper), 1, 1,
      "#sk__tiangong-choose:::" .. choice_event .. ":" .. choice_target .. ":" .. choice_effect, sk__tiangong.name, false)
    to = room:getPlayerById(to[1])
    --获得技能名
    room.tag["sk__tiangong_skills"] = room.tag["sk__tiangong_skills"] or {}
    local name = table.random(sk__tiangong_skills)
    while table.contains(room.tag["sk__tiangong_skills"], name) do
      name = table.random(sk__tiangong_skills)
    end
    --房间记录技能信息
    table.insert(room.tag["sk__tiangong_skills"], name)
    local tiangong_scope = 999
    if table.indexOf(tiangong_info.events, choice_event) > 8 then
      tiangong_scope = math.random(3)
    end
    local mark = {
      table.indexOf(tiangong_info.events, choice_event),
      table.indexOf(tiangong_info.targets, choice_target),
      table.indexOf(tiangong_info.effects, choice_effect),
      tiangong_scope,
    }
    room.tag[name] = mark
    room:handleAddLoseSkills(to, name, nil, true, false)
  end,
})
sk__tiangong:addEffect(fk.TurnEnd, {
  name = "sk__tiangong",
  anim_type = "special",
  events = { fk.GameStart, fk.TurnStart, fk.TurnEnd },
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(sk__tiangong.name) and
        (not player.room.tag["sk__tiangong_skills"] or #player.room.tag["sk__tiangong_skills"] < 7) then
      if event == fk.GameStart then
        return true
      else
        return target == player
      end
    end
  end,
  on_trigger = function(self, event, target, player, data)
    self.cancel_cost = false
    if event == fk.GameStart then
      for i = 1, 2, 1 do
        if not player:hasSkill(sk__tiangong.name) or self.cancel_cost then return end
        self:doCost(event, target, player, data)
      end
    else
      self:doCost(event, target, player, data)
    end
  end,
  on_cost = function(self, event, target, player, data)
    if player.room:askForSkillInvoke(player, sk__tiangong.name) then
      return true
    end
    self.cancel_cost = true
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    --初始化随机数
    math.randomseed(tonumber(tostring(os.time()):reverse():sub(1, 6)))
    --时机
    local nums = {}
    for i = 1, 32, 1 do
      table.insert(nums, i)
    end
    nums = table.random(nums, 3)
    local choices = {
      tiangong_info.events[nums[1]],
      tiangong_info.events[nums[2]],
      tiangong_info.events[nums[3]],
    }
    local choice_event = room:askForChoice(player, choices, sk__tiangong.name,
      "#sk__tiangong-choice_event")
    --目标
    nums = {}
    for i = 1, 26, 1 do
      table.insert(nums, i)
    end
    nums = table.random(nums, 3)
    choices = {
      tiangong_info.targets[nums[1]],
      tiangong_info.targets[nums[2]],
      tiangong_info.targets[nums[3]],
    }
    local choice_target = room:askForChoice(player, choices, sk__tiangong.name,
      "#sk__tiangong-choice_target:::" .. choice_event)
    --效果
    nums = {}
    for i = 1, 46, 1 do
      table.insert(nums, i)
    end
    nums = table.random(nums, 3)
    choices = {
      tiangong_info.effects[nums[1]],
      tiangong_info.effects[nums[2]],
      tiangong_info.effects[nums[3]],
    }
    local choice_effect = room:askForChoice(player, choices, sk__tiangong.name,
      "#sk__tiangong-choice_effect:::" .. choice_event .. ":" .. choice_target)
    --选获得技能的角色
    local to = room:askForChoosePlayers(player, table.map(room.alive_players, Util.IdMapper), 1, 1,
      "#sk__tiangong-choose:::" .. choice_event .. ":" .. choice_target .. ":" .. choice_effect, sk__tiangong.name, false)
    to = room:getPlayerById(to[1])
    --获得技能名
    room.tag["sk__tiangong_skills"] = room.tag["sk__tiangong_skills"] or {}
    local name = table.random(sk__tiangong_skills)
    while table.contains(room.tag["sk__tiangong_skills"], name) do
      name = table.random(sk__tiangong_skills)
    end
    --房间记录技能信息
    table.insert(room.tag["sk__tiangong_skills"], name)
    local tiangong_scope = 999
    if table.indexOf(tiangong_info.events, choice_event) > 8 then
      tiangong_scope = math.random(3)
    end
    local mark = {
      table.indexOf(tiangong_info.events, choice_event),
      table.indexOf(tiangong_info.targets, choice_target),
      table.indexOf(tiangong_info.effects, choice_effect),
      tiangong_scope,
    }
    room.tag[name] = mark
    room:handleAddLoseSkills(to, name, nil, true, false)
  end,
})
sk__tiangong:addEffect(fk.EventAcquireSkill, {
  can_refresh = function(self, event, target, player, data)
    if target == player then
      return table.contains(sk__tiangong_skills, data.skill.name)
    end
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    room:setPlayerMark(player, data.skill.name, room.tag[data.skill.name])
    local show = player:getTableMark("@[sk__tiangong_create]")
    table.insert(show, {
      skillName = data.skill.name,
      skillInfo = "锁定技，" .. tiangong_info.events[room.tag[data.skill.name][1]] .. "，" ..
          tiangong_info.targets[room.tag[data.skill.name][2]] ..
          tiangong_info.effects[room.tag[data.skill.name][3]] .. "。",
    })
    room:setPlayerMark(player, "@[sk__tiangong_create]", show)
  end,
})
sk__tiangong:addEffect(fk.EventLoseSkill, {
  can_refresh = function(self, event, target, player, data)
    if target == player then
      return table.contains(sk__tiangong_skills, data.name)
    end
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    local names = {}
    names = { data.skill.name }
    for _, name in ipairs(names) do
      room:setPlayerMark(player, name, 0)
      local show = player:getTableMark("@[sk__tiangong_create]")
      for i = #show, 1, -1 do
        if show[i].skillName == name then
          table.remove(show, i)
        end
      end
      if #show == 0 then show = 0 end
      room:setPlayerMark(player, "@[sk__tiangong_create]", show)
      if not table.find(room:getOtherPlayers(player), function(p)
            return p:hasSkill(name, true)
          end) then
        table.removeOne(room.tag["sk__tiangong_skills"], name)
        room.tag[name] = {}
      end
    end
  end,
})
sk__tiangong:addEffect(fk.Deathed, {
  can_refresh = function(self, event, target, player, data)
    if target == player then
      return table.find(player.player_skills, function(s)
        return table.contains(sk__tiangong_skills, s.name)
      end)
    end
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    local names = {}
    for _, s in ipairs(player.player_skills) do
      if table.contains(sk__tiangong_skills, s.name) then
        table.insert(names, s.name)
      end
    end
    for _, name in ipairs(names) do
      room:setPlayerMark(player, name, 0)
      local show = player:getTableMark("@[sk__tiangong_create]")
      for i = #show, 1, -1 do
        if show[i].skillName == name then
          table.remove(show, i)
        end
      end
      if #show == 0 then show = 0 end
      room:setPlayerMark(player, "@[sk__tiangong_create]", show)
      if not table.find(room:getOtherPlayers(player), function(p)
            return p:hasSkill(name, true)
          end) then
        table.removeOne(room.tag["sk__tiangong_skills"], name)
        room.tag[name] = {}
      end
    end
  end,
})

return sk__tiangong
